﻿#include<iostream>
using namespace std;
template<typename TR = int, typename T1, typename T2>//TR默认指定int
TR ave1(T1 a, T2 b) {
	return (a + b) / 2;
}

template<typename T1, typename T2, typename TR = T1>//TR默认指定T1
TR ave2(T1 a, T2 b) {
	return (a + b) / 2;
}

template<int max = 2000, int min = 1000, typename T>//非类型模板参数需要<>指定，写前面是因为这样可以不用定义模板参数类型
bool ChangeHp(T& hp, T damage) {
	hp -= damage;
	max = 2500;//无法过编译器，模板的参数是替换，所以参数是常量，常量无法修改值
	if (hp > max)hp = max;
	return hp < min;
}

template<typename T, T max = 2000, T min = 1000>
bool ChangeHp(T& hp, T damage) {
	hp -= damage;
	if (hp > max)hp = max;
	return hp < min;
}

template<typename T, short count>//模板数组
T ave(T(&ary)[count]) {
	T all{};
	for (int i = 0; i < count; i++) {
		all += ary[i];
	}
	return all / count;
}
int main() {
	cout << ave1<float>(100, 20) << endl;
	cout << ave2(char(1), 20) << endl;

	int hp = 2500;
	ChangeHp<2000, 1000>(hp, 100);
	ChangeHp<2000, 1000, int>(hp, 100);
	cout << hp << endl;

	int x = 1000;
	ChangeHp<x, 1000>(hp, 100);//报错，因为模板的参数是常量不是变量

	int a[5]{ 1,2,3,4,5 };
	cout << ave(a) << endl;//3

}